package com.itheima.demo6copy;

import java.io.*;

public class Copy3 {
    public static void main(String[] args) throws Exception{
        String srcPath="C:\\Users\\ASUS\\Desktop\\演示文稿1(1).jpg";
        String destPath="E:\\learn\\新建文件夹\\新建文件夹 (2)\\演示文稿1(1).jpg";
        copy(srcPath,destPath);
    }
    public static void copy(String srcPath,String destPath) {
//        创建一个文件字节输入流管道与源文件接通
        try (
             //这里只能放资源对象，用完后最终会调用close方法，自动释放资源
             InputStream is = new FileInputStream(destPath);
             OutputStream os = new FileOutputStream(srcPath);
             MyConn conn=new MyConn();
        ){
            byte[] buffer = new byte[1024];
//        定义一个变量记住每次读取的字节个数
            int len;
            while ((len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);// 把每次读取的字节数组，写入到目标文件。
            }
            System.out.println("复制完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class MyConn implements Closeable {
    @Override
    public void close() throws IOException {
        System.out.println("关闭资源");
    }
}
